package com.platform.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.article.dto.ArticleDTO;
import com.platform.article.model.ArticleDO;
import com.platform.article.service.ArticleService;
import com.platform.article.vo.ArticleInfoVO;
import com.platform.common.annotation.ApiResponseBody;
import com.platform.common.entity.PageResult;
import com.platform.common.result.ApiException;
import com.platform.common.result.ApiExceptionEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shuc
 */
@ApiResponseBody
@Controller
@RequestMapping("/manage/article")
public class ManageController {
    @Resource
    private ArticleService articleService;

    @GetMapping("/page")
    public PageResult<ArticleInfoVO> page(@RequestParam Integer page,
                                          @RequestParam Integer size) {
        Page<ArticleDO> articleDOPage = articleService.page(new Page<>(page, size));
        return PageResult.<ArticleInfoVO>builder()
                .total(articleDOPage.getTotal())
                .rows(ArticleDO.to(articleDOPage))
                .build();
    }

    @PostMapping("/update")
    public Boolean update(@RequestBody ArticleDTO articleDTO) {
        Boolean result = articleService.updateById(ArticleDO.from(articleDTO));
        if (!result) {
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        return true;
    }
}
